package com.flexfiltering.view.controls 
{
	
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;

import mx.controls.Button;
import mx.core.mx_internal;

use namespace mx_internal;

/**
 *
 */
[Style(name="auxillaryButtonStyleName", type="String", inherit="no")]

/**
 *
 */
[Style(name="backgroundOverColor", type="uint", format="Color", inherit="yes")]

/**
 *
 */
[Style(name="backgroundSelectedColor", type="uint", format="Color", inherit="yes")]

/**
 *
 */
[Event(name="auxillaryClick", type="flash.events.Event")]

public class Token extends Button
{
	
	//@ ------------------------------------------------------------ CONSTRUCTOR
	
	public function Token ()
	{
		addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
		addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
		addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
	}
	
	//@-------------------------------------------------------------- PROPERTIES
	
	//----------------------------------
	//  showAuxillaryButton
	//----------------------------------

	/**
	 *  @private
	 *  Change-flag for showAuxillaryButton property.
	 */
	private var showAuxillaryButtonChanged:Boolean;

	/**
	 *  @private
	 *  Storage for the showAuxillaryButton property.
	 */
	private var _showAuxillaryButton:Boolean;

	[Bindable()]
	/**
	 *  
	 * 
	 * 	@default 
	 */ 
	public function get showAuxillaryButton() : Boolean
	{
	    return _showAuxillaryButton;
	}

	/**
	 *  @private
	 */
	public function set showAuxillaryButton ( val:Boolean ) : void
	{
		_showAuxillaryButton = val;
		showAuxillaryButtonChanged = true;
		invalidateProperties();	
	}
	
	public var index:int;
	
	//@--------------------------------------------------------------- VARIABLES
	
	//@---------------------------------------------------------------- ELEMENTS
	
	protected var auxillaryButton:Button;
		
	//@---------------------------------------------------------- PUBLIC METHODS
	
	//@------------------------------------------------------ OVERRIDDEN METHODS

	//----------------------------------
	//  commitProperties
	//----------------------------------
	
	override protected function commitProperties():void
	{
		super.commitProperties();
		
		if ( showAuxillaryButtonChanged )
		{
			if ( !auxillaryButton && showAuxillaryButton )
			{
				auxillaryButton = new Button();
				auxillaryButton.styleName = getStyle('deleteButtonStyleName');
				addChild(auxillaryButton);
			}
			auxillaryButton.visible = showAuxillaryButton;
			showAuxillaryButtonChanged = false;
		}
	}
	
	//----------------------------------
	//  measure
	//----------------------------------
	
	override protected function measure () : void
	{
		super.measure();
		
		if ( auxillaryButton )
		{
			var horizontalGap:Number = getStyle('horizontalGap');
			measuredWidth += auxillaryButton.width + horizontalGap;
		}
	}
	
	//----------------------------------
	//  updateDisplayList
	//----------------------------------
	
	override protected function updateDisplayList( unscaledWidth:Number, 
												   unscaledHeight:Number) :void
	{
		super.updateDisplayList(unscaledWidth,unscaledHeight);
		
		var paddingRight:Number = getStyle('paddingRight');		
		
		if ( auxillaryButton )
		{
			auxillaryButton.setActualSize( auxillaryButton.getExplicitOrMeasuredWidth(),
										   auxillaryButton.getExplicitOrMeasuredHeight() );
			
			auxillaryButton.x = unscaledWidth - paddingRight - auxillaryButton.width;
			auxillaryButton.y = (unscaledHeight - auxillaryButton.height)/2;
		
			setChildIndex(DisplayObject(auxillaryButton), numChildren - 1);
		}
	}
	
	//----------------------------------
	//  viewSkinForPhase
	//----------------------------------
	
	/**
	 * 	Need to override this so that the selectedText color is displayed 
	 *	when this is selected.  I'm not sure why this isn't the default
	 * 	behavior
	 */ 
	override  mx_internal function viewSkinForPhase(tempSkinName:String, stateName:String):void
	{
		super.viewSkinForPhase(tempSkinName,stateName);
		
		if (enabled)
        {
            if(selected)

            textField.setColor(getStyle('textSelectedColor'));
        }
	}
	
	//@--------------------------------------------------------- PRIVATE METHODS
	
	//----------------------------------
	//  overAuxillaryButton
	//----------------------------------
	
	private function overAuxillaryButton (event:MouseEvent) : Boolean
	{
		var _x:Number = event.localX;
		var _y:Number = event.localY;
		
		if ( auxillaryButton )
		{
		
			return ( _x > auxillaryButton.x && _x < auxillaryButton.x + auxillaryButton.width &&
				 	 _y > auxillaryButton.y && _y < auxillaryButton.y + auxillaryButton.height )
		}
		else
		{
			return false;
		}
	}
	
	//----------------------------------
	//  setAuxillaryButtonPhase
	//----------------------------------
	
	private function setAuxillaryButtonPhase ( phase:String ) : void
	{
		if ( auxillaryButton )
		{
			auxillaryButton.phase = phase;
		}
	}
	
	//@---------------------------------------------------------- EVENT HANDLERS
	
	override protected function clickHandler(event:MouseEvent):void
	{
		if ( overAuxillaryButton (event) )
		{
			dispatchEvent(new Event("auxillaryClick",false,false));
		}
		else
		{
			super.clickHandler(event);
		}
	}
	
	protected function mouseMoveHandler ( event:MouseEvent ) : void
	{
		if ( overAuxillaryButton(event) )
		{
			setAuxillaryButtonPhase("over");
		}
		else
		{
			setAuxillaryButtonPhase("up");
		}
	}
	
	override protected function mouseDownHandler ( event:MouseEvent ) : void
	{
		if ( auxillaryButton )
		{
			if ( overAuxillaryButton(event) )
			{
				setAuxillaryButtonPhase("down");
			}
			else
			{
				setAuxillaryButtonPhase("up");
				super.mouseDownHandler(event);
			}
		}
		else
		{
			
		}
	}
	
	override protected function mouseUpHandler ( event:MouseEvent ) : void
	{
		if ( overAuxillaryButton(event) )
		{
			setAuxillaryButtonPhase("over");
		}
		else
		{
			setAuxillaryButtonPhase("up");
			super.mouseUpHandler(event);
		}
	}
	
	
} // class
} // package